<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文章列表</title>
    <link rel="stylesheet" href="/assets/lib/layui/css/layui.css">
    <link rel="stylesheet" href="/assets/css/article/articleList.css">
</head>

<body>
    <div class="layui-card">
        <div class="layui-card-header">文章列表</div>
        <div class="layui-card-body">
            <!-- 条件筛选 -->
            <form class="layui-form" lay-filter="formDemo" id="formBox">
                <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
                <div class="layui-form-item layui-inline">
                    <select name="cate_id" id="classifyBox">
                        <option value="">请选择文章分类</option>
                    </select>
                </div>

                <script type="text/html" id='classifyTemp'>
                    <option value="">请选择文章分类</option>
                    {{each data}}
                    <option value={{$value.id}}>{{$value.name}}</option>
                    {{/each}}
                </script>

                <div class="layui-form-item layui-inline">
                    <select name="state">
                        <option value="">请选择发布状态</option>
                        <option value="草稿">草稿</option>
                        <option value="已发布">已发布</option>

                    </select>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="layui-btn" lay-submit>筛选</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </form>
            <!-- 文章列表 -->
            <table class="layui-table">
                <colgroup>
                    <col>
                    <col width="150">
                    <col width="300">
                    <col width="100">
                    <col width="150">
                </colgroup>
                <thead>
                    <tr>
                        <th>文章标题</th>
                        <th>分类</th>
                        <th>发表时间</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                   
                </tbody>
            </table>
            <!-- 页码 -->
            <div id="test1"></div>
        </div>
    </div>
    <script type="text/html" id="Tp1">
        {{each data}}
        <tr>
            <td>{{$value.title}}</td>
            <td>{{$value.cate_name}}</td>
            <td>{{dataFormat($value.pub_date) }}</td>
            <td>{{$value.state}}</td>
            <td>
                <button type="button" class="layui-btn layui-btn layui-btn-normal layui-btn-sm"  data-id="{{$value.Id}}" id="editArt">编辑</button>
                <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" data-id="{{$value.Id}}" id="delArticle">删除</button>
            </td>
          </tr>
          {{/each}}
    </script>
    <script src="/assets/lib/layui/layui.all.js"></script>
    <script src="/assets/lib/jquery.js"></script>
    <script src="/assets/js/baseApi.js"></script>
    <script src="/assets/lib/template-web.js"></script>
    <script>
        $(function () {
            const { form, layer } = layui
            //定义美化时间的过滤器
            template.defaults.imports.dataFormat = function (date) {
                const dt = new Date(date)
                const y = dt.getFullYear()
                const m = dt.getMonth() + 1
                const d = dt.getDay()

                const hh = dt.getHours()
                const mm = dt.getMinutes()
                const ss = dt.getSeconds()
                return y + '-' + padZero(m) + '-' + padZero(d) + ' ' + hh + ':' + mm + ':' + ss
                function padZero(n) { return n > 9 ? n : '0' + n }
            }
            //请求参数对象
            const data = {
                pagenum: 1,
                pagesize: 2,
                cate_id: '',
                state: ''
            }
            //初始化表格数据
            function initTable() {
                $.ajax({
                    type: 'get',
                    url: '/my/article/list',
                    data,
                    success(res) {
                        console.log(res);
                        if (res.status === 0) {
                            const html = template('Tp1', { data: res.data });
                            //   console.log(html);
                            $("tbody").html(html);
                            renderPage(res.total);//渲染分页
                        }
                    }
                })
            }
            //初始化文章分类
            function initClassify() {
                $.ajax({
                    type: 'get',
                    url: '/my/article/cates',
                    success(res) {
                        console.log(res);
                        const html = template('classifyTemp', { data: res.data })
                        // console.log(html);
                        $("#classifyBox").html(html);
                        form.render();//重新渲染
                    }
                })
            }
            initTable();
            initClassify();
            //删除文章
            $("body").on('click', "#delArticle", function () {
                console.log($(this).attr('data-id'));
                const id = $(this).attr('data-id');
                layer.confirm('您确定要删除该文章吗?', function (index) {
                    //do something
                    $.ajax({
                        type: 'get',
                        url: '/my/article/delete/' + id,
                        success(res) {
                            console.log(res);
                            layer.msg(res.message);
                            if (res.status === 0) {
                                if (data.pagenum > 1 && $('tbody tr').length === 1) {
                                    data.pagenum--
                                }
                                initTable();
                            }
                        }
                    })
                    layer.close(index);
                });
            })
            //提交筛选
            $("#formBox").on('submit', function (e) {
                e.preventDefault();
                console.log(form.val('formDemo'));
                const formDemo = form.val('formDemo');
                data.cate_id = formDemo.cate_id;
                data.state = formDemo.state;
                console.log(data);
                initTable();
            })
            //分页
            function renderPage(total) {
                layui.use('laypage', function () {
                    var laypage = layui.laypage;
                    console.log(total);

                    //执行一个laypage实例
                    laypage.render({
                        elem: 'test1', //注意，这里的 test1 是 ID，不用加 # 号
                        count: total, //数据总数，从服务端得到
                        limit: data.pagesize,//每页数据条数
                        limits: [2, 5, 8, 10],//每页条数的选项
                        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
                        curr: data.pagenum,//当前选中页
                        jump(obj, first) {
                            // console.log(obj,first);                           
                            if (!first) {
                                console.log(obj.curr);
                                console.log(obj.limit);
                                data.pagenum = obj.curr;
                                data.pagesize = obj.limit;
                                initTable();
                            }
                        }
                    });
                });
                form.render()
            }
            //编辑
            $("body").on('click',"#editArt",function(){
                console.log('111');
                console.log($(this).attr('data-id'));
                const aid = $(this).attr('data-id');
                location.href = '/article/editArt.html?uid='+aid;
            })
        })
    </script>
</body>

</html>